package likou.jindian;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/03/15 07:28
 */
public class _16_02_1 {
    Node[] nodes = new Node[2 << 16];
    int len = nodes.length - 1;

    static class Node {
        String key;
        int value;
        Node next;

        public Node(String key) {
            this.key = key;
            value = 1;
        }
    }

    void put(String key) {
        int hash = key.hashCode();
        int index = hash & len;
        if (nodes[index] == null) {
            nodes[index] = new Node(key);
        } else {
            Node node = nodes[index];
            while (node.next != null && !key.equals(node.key)) {
                node = node.next;
            }
            if (key.equals(node.key)) {
                node.value = node.value + 1;
            } else {
                node.next = new Node(key);
            }
        }
    }

    public _16_02_1(String[] book) {
        for (String word : book) {
            put(word);
        }
    }

    public int get(String word) {
        int index = word.hashCode() & len;
        Node node = nodes[index];
        while (node != null) {
            if (word.equals(node.key)) {
                return node.value;
            }
            node = node.next;
        }
        return 0;
    }

}
